import { wxRequest } from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    status: '',
    date: '',
    uploadUrl: wx.getStorageSync('uploadUrl'),
    showModal: false,
    tabId: 1,
    pageNo:1,
    hasNextPage: false,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {
    this.getlist()
  },


  //tab切换
  handleTab: function (e) {
    var tabId = e.currentTarget.dataset.id
    this.setData({
      tabId
    })
    this.getlist()
  },

  //日期
  bindPickerChange: function (e) { 
    this.setData({
      cancelRequestDate: e.detail.value
    })
  },


  //订单详情
  toDtail: function (e) {
    wx.navigateTo({
      url: '/pages/menber/mall/detail?id=' + e.currentTarget.dataset.id + "&typefg=1",
    })
  },


  //查询列表
  getlist: function () {
    // &subStatus=43
    var url = '/odr/protected/order/findRefundList'
    // if (this.data.tabId == 2) {
    //   url = '/odr/protected/orgOrder/findAll'
    // }

    var params={
      pageNo: this.data.pageNo, 
      status: this.data.status,
      receiverMobile: this.data.keyword || ''
    }
    var cancelRequestDate=this.data.cancelRequestDate;
    console.log(cancelRequestDate,'cancelRequestDate')
    if (this.data.cancelRequestDate){
      params={
        cancelRequestDate:cancelRequestDate,
        ...params
      }
    }
 

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      contentType: 'application/x-www-form-urlencoded',
      data: params
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }
      list.map(item=>{
        if (typeof item.detailJson == 'string') {
          item.detailJson = JSON.parse(item.detailJson)
        }
      })
      console.log(list,'list')
      this.setData({
        list: list,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

    })
  },



  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },
  
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },



  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },

  popupConfirm: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();
  },
  //选择状态
  checkStatus: function (e) { 
    var status = e.currentTarget.dataset.id
    this.setData({
      status
    })
  },

  // orgNameInput: function (e) {
  //   var orgName = e.detail.value;
  //   console.log(orgName, 'orgName')
  //   this.setData({
  //     orgName
  //   })
  // },

  // clearorgNameInput: function () {
  //   this.setData({
  //     orgName: ''
  //   })
  // },

  //********************筛选更多end********************




  //同意退款
  agreed: function (e) {
    var item = e.currentTarget.dataset.item
    console.log(item,'agreedagreed')
    var params = {
      "refId": item.refId,  
      "auditStatus": 1,
      "auditMemo": "同意"
    }

    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function (res) {
        if (res.confirm) {
          that.auditRequest(params)
        }
      }
    })
  },


  //不同意
  unAgreedInit: function (e) {
    var showModal = this.data.showModal;
    var item = e.currentTarget.dataset.item
    this.setData({
      showModal: !showModal,
      auditMemo: '',
      unAgreedItem: item
    })
  },


  //监听理由
  onReason: function (e) {
    var inputval = e.detail.value;
    console.log(inputval,'inputval')
    this.setData({
      auditMemo: inputval
    })
  },

  //取消
  cancel: function () {
    var showModal = this.data.showModal;
    this.setData({
      showModal: !showModal,
      auditMemo: '',
      unAgreedItem: ''
    })
  },

  //确定
  confirm: function () {
    if (!this.data.auditMemo) {
      wx.showToast({
        icon: 'none',
        title: '理由不能为空',
      })
      return
    }
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function (res) {
        if (res.confirm) {
          that.setData({
            showModal: false,
          })
          //参数
          if (that.data.unAgreedItem) {
            var params = { 
              "refId": that.data.unAgreedItem.refId, 
              "auditStatus": 2,
              "auditMemo": that.data.auditMemo
            }
            that.auditRequest(params);
          }
        }
      }
    })
  },

  //审核
  auditRequest: function (params) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/refundAudit?refId=' + params.refId + '&auditStatus=' + params.auditStatus + '&auditMemo=' + params.auditMemo,
      method: 'POST'
    }, (res) => {
      wx.showToast({
        icon: 'succeed',
        title: '操作成功',
      })
      setTimeout(function(){
        that.getlist()
      },1200)
      
    })
  }









})